package com.yyjj.zixun.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yyjj.zixun.R;
import com.yyjj.zixun.adapter.JecaoAdapter;
import com.yyjj.zixun.entity.VideoEntity;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerManager;
import tech.com.commoncore.app.FastManager;
import tech.com.commoncore.base.BaseRefreshLoadActivity;

/**
 * Anthor:NiceWind
 * Time:2019/3/26
 * Desc:The ladder is real, only the climb is all.
 */
@Route(path = "/zixun/video")
public class VideoActivity extends BaseRefreshLoadActivity {
    public String[] tvs = {
            "把握区块链途径一：成为项目方的一员-国语流畅",
            "主流交易所的使用流程和注意事项-国语流畅",
            "清晰认识数字货币-国语流畅",
            "数字货币钱包的发展与分类-国语流畅",
            "区块链专业术语解析-国语流畅",
            "亚洲金融论坛：「区块链技术」创造新商业价值",
    };
    private String host= "http://stock.yingju8.com/video/";
    private List<VideoEntity> videoLists = new ArrayList<>();

    @Override
    public BaseQuickAdapter getAdapter() {
        return new JecaoAdapter(R.layout.zixun_item_video);
    }

    @Override
    public void loadData(int page) {
        if(page==0){
            for (String tv : tvs) {
                String onlineUrl = host + tv +".mp4";
                VideoEntity entity = new VideoEntity(
                        tv,
                        null,
                        onlineUrl,
                        tv.equals(tvs[0])?VideoEntity.TYPE_BIG:VideoEntity.TYPE_NOMAL);

                videoLists.add(entity);
            }
            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),videoLists,null);
        }else{
            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),null,null);
        }

    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("财经视频").setTextColor(Color.WHITE).setLeftTextDrawable(R.mipmap.back_white).setBackground(getResources().getDrawable(R.drawable.btn_jb));
    }

    @Override
    public int getContentLayout() {
        return R.layout.zixun_activity_refresh_titlebar;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                if (JCVideoPlayerManager.getCurrentJcvd() != null) {
                    JCVideoPlayer videoPlayer =  JCVideoPlayerManager.getCurrentJcvd();
                    if (((ViewGroup) view).indexOfChild(videoPlayer) != -1 && videoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
                        JCVideoPlayer.releaseAllVideos();
                    }
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
